import UIKit

class TAMTLanuchController: UIViewController {
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }

    // 创建一个标签，用来显示启动动画的文字
    private let label: UILabel = {
        let label = UILabel()
        label.text = "他遇\n\n相识\n相知\n相聊\n\n遇见美好，从这里开始"
        label.font = UIFont.systemFont(ofSize: 30, weight: .bold)
        label.textColor = .white
        label.textAlignment = .center
        label.numberOfLines = 0
        label.alpha = 0 // 初始设置为透明
        return label
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        let imageView = UIImageView(image: UIImage(named: "bg"))
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.frame = self.view.bounds
        self.view.addSubview(imageView)
        
        // 设置背景色为渐变色
//        let gradientLayer = CAGradientLayer()
//        gradientLayer.frame = view.bounds
//        gradientLayer.colors = [UIColor.blue.cgColor, UIColor.purple.cgColor]
//        view.layer.addSublayer(gradientLayer)
        
        // 将标签添加到视图中
        view.addSubview(label)
        
        // 设置标签的布局
        label.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            label.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
            label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20)
        ])
        
        // 启动动画
        animateText()
    }

    // 文字逐渐显示的动画，增加丰富效果
    private func animateText() {
        let fullText = label.text ?? ""
        label.text = "" // 清空文本
        
        // 设置一个时间间隔来逐字显示
        var currentIndex = 0
        Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
            if currentIndex < fullText.count {
                let index = fullText.index(fullText.startIndex, offsetBy: currentIndex)
                self.label.text?.append(fullText[index])
                self.applyTextAnimation(at: currentIndex)
                currentIndex += 1
            } else {
                timer.invalidate()
                self.showMainScreen()
            }
        }
        
        // 渐变显示文字
        UIView.animate(withDuration: 1.0, animations: {
            self.label.alpha = 1.0
        })
    }

    private func applyTextAnimation(at index: Int) {
        let fullText = label.text ?? ""
        
        // 确保索引在有效范围内
        if index < fullText.count {
            // 获取当前字符
            let character = fullText[fullText.index(fullText.startIndex, offsetBy: index)]
            
            // 创建一个临时标签显示当前字符
            let characterLabel = UILabel()
            characterLabel.text = String(character)  // 转换单个字符为字符串
            characterLabel.font = label.font
            characterLabel.textColor = UIColor.white
            characterLabel.alpha = 0
            
            // 设置动画效果：缩放+透明度变化
            let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")
            scaleAnimation.fromValue = 0.5
            scaleAnimation.toValue = 1.0
            scaleAnimation.duration = 0.3
            
            let fadeInAnimation = CABasicAnimation(keyPath: "opacity")
            fadeInAnimation.fromValue = 0
            fadeInAnimation.toValue = 1
            fadeInAnimation.duration = 0.3

            // 创建一个组动画同时应用缩放和透明度
            let groupAnimation = CAAnimationGroup()
            groupAnimation.animations = [scaleAnimation, fadeInAnimation]
            groupAnimation.duration = 0.5
            groupAnimation.beginTime = CACurrentMediaTime() + Double(index) * 0.1 // 每个字符延时

            characterLabel.layer.add(groupAnimation, forKey: nil)

            // 动画结束后移除标签
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                characterLabel.removeFromSuperview()
            }
            
            // 将字符标签添加到视图中
            view.addSubview(characterLabel)
        }
    }


    // 启动动画结束后，跳转到主界面
    private func showMainScreen() {
        // 模拟启动动画后切换到主界面
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            let mainViewController = TAMTIndexController() // 你的主界面
            mainViewController.modalTransitionStyle = .flipHorizontal
            mainViewController.modalPresentationStyle = .fullScreen
            self.present(mainViewController, animated: true, completion: nil)
        }
    }
}
